package stage1.module2.Q2;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        final int SIZE = 16;

        Scanner s = new Scanner(System.in);

        Board board = new Board(SIZE);
        board.printBoard();
        char currentTurn = 'b';
        String row, col;

        // 开始游戏
        while (true) {
            // 打印出当前的玩家
            String msg = currentTurn == 'b' ? "Player black is playing this turn" : "Player white is playing this turn";
            System.out.println(msg);

            while (true) {
                System.out.println("Choose row:");
                row = s.nextLine();
                System.out.println("Choose col:");
                col = s.nextLine();

                // 当前玩家尝试落子
                int status = board.putPiece(currentTurn, row, col);
                if (status == -1) {
                    System.err.println("The position you give is invalid, please try again");
                }
                else if (status == 1) {
                    System.err.println("The given position has already been occupied, try to give a new one");
                }
                else {
                    // 玩家提供了有效的位置，棋子成功放置到棋盘上
                    break;
                }
            }

            // 打印出当前状态的棋盘
            board.printBoard();

            // 检查赢家
            if (board.checkWinner(currentTurn, row, col)) {
                msg = currentTurn == 'b' ? "Player black has won the game!" : "Player black has won the game!";
                System.out.println(msg);
                break;
            }

            // 轮换玩家
            currentTurn = currentTurn == 'b' ? 'w' :'b';
        }
    }
}
